package com.vidmt.api.common.pojo;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class ApiResultClient<T> {
	private int code;
	private String msg;
	private JSONObject rawJson;
	private Class<T> clz;

	public ApiResultClient(String jsonstr) {
		this(jsonstr, (Class<T>) String.class);
	}

	public ApiResultClient(String jsonstr, Class<T> clz) {
		rawJson = JSON.parseObject(jsonstr);
		this.code = rawJson.getIntValue("c");
		this.msg = rawJson.getString("m");
		this.clz = clz;
	}

	public int getCode() {
		return code;
	}

	public String getMsg() {
		return msg;
	}

	public T obtainData() {
		return rawJson.getObject("d", clz);
	}

	public List<T> obtainList() {
		JSONArray jarr = rawJson.getJSONArray("d");
		if (jarr == null) {
			return null;
		}
		return JSONArray.parseArray(jarr.toJSONString(), clz);
	}

	@Override
	public String toString() {
		return JSON.toJSONString(this);
	}
}
